<template>
  <div class="app-container">
    <el-row :gutter="10">
      <!--数据库名称数据-->
      <el-col :span="4" :xs="24">
        <el-card class="box-card">
          <div class="head-container">
            <el-input
              v-model="entityName"
              placeholder="请输入实体名称"
              clearable
              size="small"
              prefix-icon="el-icon-search"
              style="margin-bottom: 20px"
            />
          </div>
          <div class="head-container">
            <el-tree
              :data="entityOptions"
              :props="defaultProps"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              ref="tree"
              default-expand-all
              @node-click="handleNodeClick"
            />
          </div>
        </el-card>
      </el-col>
      <!--数据表数据-->
      <el-col :span="20" :xs="24">
        <el-card class="box-card">
          <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="名称" prop="name">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入名称"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPerm="['trigger_add']"
              >新增
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="success"
                icon="el-icon-edit"
                size="mini"
                :disabled="single"
                @click="handleUpdate"
                v-hasPerm="['trigger_edit']"
              >修改
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPerm="['trigger_del']"
              >删除
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="warning"
                icon="el-icon-download"
                size="mini"
                @click="handleExport"
                v-hasPerm="['trigger_export']"
              >导出
              </el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="triggerList" border @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="编号" width="55" align="center" prop="id"/>
            <el-table-column label="所属实体" align="center" prop="entityName"/>
            <el-table-column label="名称" align="center" prop="name"/>
            <el-table-column label="触发类型" align="center" prop="actionType"/>
            <!--            <el-table-column label="触发ID" align="center" prop="actionConfigId"/>-->
            <!--            <el-table-column label="动作内容" align="center" prop="actionContent"/>
                        <el-table-column label="过滤条件" align="center" prop="actionFilter"/>
                        <el-table-column label="cron表达式" align="center" prop="cronExp"/>-->
            <el-table-column label="优先级" align="center" prop="priority"/>
            <el-table-column label="是否禁用" prop="isDisabled" align="center" width="100">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.yes_no" :value="scope.row.isDisabled"/>
              </template>
            </el-table-column>
            <!--            <el-table-column label="异常抛出" prop="isThrow" align="center" width="100">
                          <template slot-scope="scope">
                            <dict-tag :options="dict.type.yes_no" :value="scope.row.isThrow"/>
                          </template>
                        </el-table-column>-->
            <el-table-column label="创建时间" align="center" prop="createTime" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPerm="['trigger_edit']"
                >修改
                </el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPerm="['trigger_del']"
                >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.current"
            :limit.sync="queryParams.size"
            @pagination="getList"
          />
        </el-card>
      </el-col>
    </el-row>
    <!-- 添加或修改触发器对话框 -->
    <h5ve-dialog :title="title" :show.sync="open" :isFooter="true" :width="500">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="所属实体" prop="entityId">
              <treeselect
                v-model="form.entityId"
                :options="entityAddOptions"
                :normalizer="normalizer"
                :show-count="true"
                placeholder="请输入所属实体"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="触发ID" prop="actionConfigId">
              <el-input v-model="form.actionConfigId" placeholder="请输入触发ID"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称"/>
            </el-form-item>
          </el-col>
          <!--          <el-col :span="12">
                      <el-form-item label="动作内容" prop="actionContent">
                        <el-input v-model="form.actionContent" placeholder="请输入动作内容"/>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="过滤条件" prop="actionFilter">
                        <el-input v-model="form.actionFilter" placeholder="请输入过滤条件"/>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="cron表达式" prop="cronExp">
                        <el-input v-model="form.cronExp" placeholder="请输入cron表达式"/>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="优先级" prop="priority">
                        <el-input v-model="form.priority" placeholder="请输入优先级"/>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="是否禁用">
                        <el-radio-group v-model="form.isDisabled">
                          <el-radio
                            v-for="dict in dict.type.yes_no"
                            :key="dict.value"
                            :label="dict.value"
                          >{{ dict.label }}
                          </el-radio>
                        </el-radio-group>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="异常抛出">
                        <el-radio-group v-model="form.isThrow">
                          <el-radio
                            v-for="dict in dict.type.yes_no"
                            :key="dict.value"
                            :label="dict.value"
                          >{{ dict.label }}
                          </el-radio>
                        </el-radio-group>
                      </el-form-item>
                    </el-col>-->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </h5ve-dialog>
  </div>
</template>

<script>
import {addTrigger, delTrigger, editTrigger, getTrigger, listEntity, listTrigger} from "@/api/toolkit/trigger";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Trigger",
  dicts: ['yes_no'],
  components: {Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 触发器表格数据
      triggerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否禁用字典
      isDisabledOptions: [],
      // 异常抛出字典
      isThrowOptions: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        name: null,
        entityName: null,
        entityId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      entityName: null,
      entityOptions: [{tableComment: "全部实体", id: 0}],
      entityAddOptions: [],
      defaultProps: {
        children: "children",
        label: "tableComment"
      },
    };
  },
  created() {
    this.getList();
    this.getEntityList();
  },
  methods: {
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.tableComment,
        children: node.children
      };
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.tableComment.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      if (data.id !== 0) {
        this.queryParams.entityId = data.id;
        data.entityId = data.id;
      } else {
        this.queryParams.entityId = null;
        data.entityId = null;
      }
      this.getList();
    },
    //获取实体信息
    getEntityList() {
      listEntity().then(res => {
        console.log(res.data)
        this.entityOptions = this.entityOptions.concat(res.data);
        console.log(this.entityOptions);
        this.entityAddOptions = res.data;
      })
    },
    /** 查询触发器列表 */
    getList() {
      this.loading = true;
      listTrigger(this.queryParams).then(response => {
        this.triggerList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        entityId: null,
        name: null,
        actionType: null,
        actionConfigId: null,
        actionContent: null,
        actionFilter: null,
        cronExp: null,
        priority: null,
        isDisabled: "0",
        isThrow: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remarks: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加触发器";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTrigger(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改触发器";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            editTrigger(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTrigger(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除触发器编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delTrigger(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('toolkit/trigger/export', {
        ...this.queryParams
      }, "触发器_" + new Date().getTime() + ".xlsx")
    }
  }
};
</script>
